//
//  CZStatusPictureView.swift
//  iOSWeibo10
//
//  Created by ge on 16/5/17.
//  Copyright © 2016年 ge. All rights reserved.
//

import UIKit

/// 微博配图,9宫格的方式显示
/*
1.添加9张imageView
2.根据url来布局位置显示图片,不显示图片的imageView就隐藏掉
*/

/// 最大列数
private let MaxColumn = 3

/// imageView之间的间距
private let ImageViewMargin: CGFloat = 4

// 计算一个图片的宽度和高度
// (屏幕的宽度 - 2 * CellMargin - (MaxColumn - 1) * ImageViewMargin) / MaxColumn
//MaxColumn和CZStatusPictureViewImageViewWH不确定创建谁先，所以放到外面，在外面就没这种情况
private let CZStatusPictureViewImageViewWH = (UIScreen.mainScreen().bounds.width - 2 * CellMargin - (CGFloat(MaxColumn) - 1) * ImageViewMargin) / CGFloat(MaxColumn)

class CZStatusPictureView: UIView {
    


    
    /// 要显示图片的url数组
    var pictureURLs: [NSURL]? {
        didSet {
            // 原创view设置配图url后
            let size = layoutImages(pictureURLs)
            
            // 重新更新约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
        }
    }
    
    /// 布局9个imageView
    private func layoutImages(pictureURLs: [NSURL]?) -> CGSize {
        
        /// 要显示图片的数量
        let count = pictureURLs?.count ?? 0
        
        // 测试count
//        let count = 1
        
        debugLabel.text = "\(count)"
        
        /// 3列
        var numberOfColumn = 3
        
        if count == 1 {
            numberOfColumn = 1
        } else if count == 2 || count == 4 {
            numberOfColumn = 2
        }
        
        // 遍历每一个imageView,设置frame
        var index = 0
        for imageView in imageViews {
            if index >= count {
                // 隐藏
                imageView.hidden = true
            } else {
                // 显示, 布局
                imageView.hidden = false
            
                // 计算每个imageView在哪列
                let column = index % numberOfColumn
                
                // 计算每个imageView在哪行
                let row = index / numberOfColumn
                
                // 计算x
                let x = CGFloat(column) * (CZStatusPictureViewImageViewWH + ImageViewMargin)
                
                // 计算y
                let y = CGFloat(row) * (CZStatusPictureViewImageViewWH + ImageViewMargin)
                
                // 设置imageView的fame
                imageView.frame = CGRect(x: x, y: y, width: CZStatusPictureViewImageViewWH, height: CZStatusPictureViewImageViewWH)
                
                // 设置图片
                let url = pictureURLs![index]
                imageView.sd_setImageWithURL(url)
            }
            
            index++
        }
        
        // 计算配图的大小
        if count == 0 {
            return CGSizeZero
        }
        
        // 总行数 = (count + 列数 - 1) / 列数  // 公式, 知道数据的总条数,一页显示多少条,算多少页
        // 1 = (3 + 3 - 1) / 3
        // 2 = (5 + 3 - 1) / 3
        // 3 = (8 + 3 - 1) / 3
        let numberOfRow = (count + numberOfColumn - 1) / numberOfColumn
        // width = 列数 * imageView.width + (列数 - 1) * 间距
        let width = CGFloat(numberOfColumn) * CZStatusPictureViewImageViewWH + (CGFloat(numberOfColumn) - 1) * ImageViewMargin
        // height = 行数 * imageView.height + (行数 - 1) * 间距
        let height = CGFloat(numberOfRow) * CZStatusPictureViewImageViewWH + (CGFloat(numberOfRow) - 1) * ImageViewMargin
        
        return CGSize(width: width, height: height)
    }
    
    // MARK: - 属性
    // 存放9个imageView
    private var imageViews = [UIImageView]()
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    private func prepareUI() {
        backgroundColor = UIColor.clearColor()
        
        // 添加9个imageView
        for _ in 0..<9 {
            // 创建imageView
            let imageView = UIImageView()
            imageView.backgroundColor = UIColor.randomColor()
            // 设置填充模式
            imageView.contentMode = UIViewContentMode.ScaleAspectFill
            imageView.clipsToBounds = true
            
            self.addSubview(imageView)
            // 添加到imageViews的数组里面,方便后续操作
            imageViews.append(imageView)
        }
        
        addSubview(debugLabel)
    }
    
    //MARK: - 懒加载
    private lazy var debugLabel: UILabel = {
    
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(20)
        label.textColor = UIColor.blackColor()
        label.text = "我还没有图片"
        label.frame = CGRect(x: 20, y: 20, width: 200, height: 100)
        
        return label
    }()

}






